package com.ljf.game.event;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ljf.game.WebsocketManager;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 描述 :
 *
 {
 "EVENT_CLASS":"CLOSE_ROOM"
 }
 * <p>
 * 版本     作者     时间      内容
 * 1.0      lijinfeng       2025-04-29 14:32     create
 */
public class CloseRoomEvent extends AbstractEvent {


    public void run(WebSocketSession session,String text) {
        JSONObject jsonObject =  JSON.parseObject(text);
        String homeowner = jsonObject.get("HOME_OWNER").toString();
        HashMap<String, Object> hashMap = EventManager.getResultMap(text);
        String username = WebsocketManager.getUsername(session);

        if (!username.equals(homeowner)){
            WebsocketManager.sendMessage(session, hashMap, "房主不是 [" + username + "]，不可以关闭房间");
            return;
        }

        ConcurrentHashMap<String, WebSocketSession> roomMap =  WebsocketManager.groupGet(username);
        if(null == roomMap || roomMap.isEmpty()){
            WebsocketManager.sendMessage(session, hashMap, "房间 [" + username + "] 不存在");
            return;
        }
        String message = username + " 关闭房间";
        WebsocketManager.sendMessageToAll(roomMap, hashMap, message);
        WebsocketManager.groupRemove(username);

    }
}
